package com.ty.controller;

import com.ty.bean.BootStrapResultData;
import com.ty.bean.BootStrapTableCourier;
import com.ty.bean.Courier;
import com.ty.bean.Message;
import com.ty.mvc.ResponseBody;
import com.ty.service.BaseCourierService;
import com.ty.service.impl.BaseCourierServiceImpl;
import com.ty.util.DateFormatUtil;
import com.ty.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 田野
 */
public class CourierController {

    BaseCourierService service = new BaseCourierServiceImpl();

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Integer> console = service.console();

        // session存储快递员总数值
        HttpSession session = request.getSession();
        Integer totalCouriers = console.get("couriers");
        session.setAttribute("totalCouriers", totalCouriers);

        Message message = new Message();
        if (console != null && console.size() > 0) {
            // 有数据
            message.setStatus(0);
            message.setData(console);
        } else {
            // 没有数据
            message.setStatus(-1);
        }

        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        // 1、获取参数（前台发过来的数据，例如现在是第几页，一共需要多少个数据等）
        // 获取查询数据的起始索引值
        String offset = request.getParameter("offset");
        // 获取当前页要显示的数据量
        String pageNumber = request.getParameter("pageNumber");
        List<BootStrapTableCourier> list = new ArrayList<>();

        if (offset != null && pageNumber != null) {
            List<Courier> couriers = service.findAll(true, Integer.parseInt(offset), Integer.parseInt(pageNumber));
            if (couriers != null && couriers.size() > 0) {
                for (Courier courier : couriers) {
                    BootStrapTableCourier bootStrapTableCourier = new BootStrapTableCourier();
                    bootStrapTableCourier.setId(courier.getId());
                    bootStrapTableCourier.setName(courier.getName());
                    bootStrapTableCourier.setPhone(courier.getPhone());
                    bootStrapTableCourier.setIdCard(courier.getIdCard());
                    bootStrapTableCourier.setPassword(courier.getPassword());
                    bootStrapTableCourier.setDeliveries(courier.getDeliveries());
                    if (courier.getRegTime() != null) {
                        String regTime = DateFormatUtil.format(courier.getRegTime());
                        bootStrapTableCourier.setRegTime(regTime);
                    }
                    if (courier.getLoginTime() != null) {
                        String loginTime = DateFormatUtil.format(courier.getLoginTime());
                        bootStrapTableCourier.setLoginTime(loginTime);
                    }
                    bootStrapTableCourier.setInfo(courier.getInfo());
                    list.add(bootStrapTableCourier);
                }

                BootStrapResultData<BootStrapTableCourier> data = new BootStrapResultData<>();
                data.setRows(list);
                // session取值
                HttpSession session = request.getSession();
                Integer total = (Integer) session.getAttribute("totalCouriers");

                data.setTotal(total);

                String json = JSONUtil.toJSON(data);
                return json;
            }
        }
        return null;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        // 1、获取参数
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String idCard = request.getParameter("idCard");
        String password = request.getParameter("password");
        String info = request.getParameter("info");
        // 2、调用Service层方法
        Courier courier = new Courier();
        courier.setName(name);
        courier.setPhone(phone);
        courier.setIdCard(idCard);
        courier.setPassword(password);
        courier.setInfo(info);

        boolean insert = service.insert(courier);

        // 3、包装并返回结果给Servlet，Servlet再返回给前端
        Message message = new Message();
        if (insert) {
            // session取值
            HttpSession session = request.getSession();
            Integer total = (Integer) session.getAttribute("totalCouriers");
            session.setAttribute("totalCouriers", total + 1);

            message.setStatus(0);
            message.setResult("新增成功！");
        } else {
            message.setStatus(-1);
            message.setResult("新增失败！");
        }

        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        String phone = request.getParameter("phone");
        Courier byPhone = service.findByPhone(phone);
        Message message = new Message();
        if (byPhone != null) {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(byPhone);
        } else {
            message.setStatus(-1);
            message.setResult("所查快递员信息不存在");
        }

        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        String courierId = request.getParameter("courierId");
        String editName = request.getParameter("editName");
        String editPhone = request.getParameter("editPhone");
        String editIdCard = request.getParameter("editIdCard");
        String editPassword = request.getParameter("editPassword");

        Courier courier = new Courier();

        courier.setName(editName);
        courier.setPhone(editPhone);
        courier.setIdCard(editIdCard);
        courier.setPassword(editPassword);

        Message message = new Message();
        boolean update = false;
        if (courierId != null) {
            courier.setId(Integer.parseInt(courierId));
            update = service.update(Integer.parseInt(courierId), courier);
        }

        if (update) {
            message.setStatus(0);
            message.setResult("更新成功！");
        } else {
            message.setStatus(-1);
            message.setResult("更新失败！");
        }

        String json = JSONUtil.toJSON(message);
        return json;

    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String courierId = request.getParameter("courierId");
        Message message = new Message();
        boolean delete = false;
        if (courierId != null) {
            delete = service.delete(Integer.parseInt(courierId));
        }

        if (delete) {
            // session更新
            // session取值
            HttpSession session = request.getSession();
            Integer total = (Integer) session.getAttribute("totalCouriers");
            session.setAttribute("totalCouriers", total - 1);


            message.setStatus(0);
            message.setResult("删除成功！");
        } else {
            message.setStatus(-1);
            message.setResult("删除失败！");
        }

        String json = JSONUtil.toJSON(message);

        return json;

    }


}
